package com.songshuai.pangu.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.songshuai.pangu.api.dto.pangu.UploadOssResDto;
import com.songshuai.pangu.api.service.CommonService;
import com.songshuai.webapi.baseapi.constant.BaseConstant;
import com.songshuai.webapi.baseapi.dto.feign.response.custminiprogram.UploadImgResDto;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.enumerations.sales.error.SopImgError;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.QueryRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.utils.DateUtil;
import com.songshuai.webapi.baseapi.utils.QiNiuUploadUtil;
import com.songshuai.webapi.baseapi.utils.SnowFlakeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
import java.util.Map;

/**
 * @Author:wbl
 * @Date: 2019/3/27
 */
@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private SnowFlakeUtil snowFlakeUtil;

    @Autowired
    private QiNiuUploadUtil qiNiuUploadUtil;

    @Value("${qiniuyun.domain}")
    private String domain;

    @Override
    public QueryResponse uploadPicture(MultipartFile file, Integer imgMaxSize, String basePath, String imgPath) throws Exception{
        if (null == file) {
            return new QueryResponse(SopImgError.ERROR_NO_SELECTED_TYPE.getCode(), SopImgError.ERROR_NO_SELECTED_TYPE.getMsg());
        }
        if (!checkImgSize(file,imgMaxSize)) {
            return new QueryResponse(SopImgError.ERROR_IMG_SIZE.getCode(), SopImgError.ERROR_IMG_SIZE.getMsg());
        }
        String fileName = file.getOriginalFilename();  //文件名字
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String imgName = snowFlakeUtil.nextId() + suffix;  //生成新的文件名
        //文件名前加上配置的路径
        if (StringUtils.isNotEmpty(imgPath)) {
            imgName = imgPath + BaseConstant.DEFAULT_URL_SEPARATOR + imgName;
        }
        imgName = DateUtil.ChangeDateToString(new Date(), DateUtil.dateFormat4) + BaseConstant.DEFAULT_URL_SEPARATOR + imgName;
        if (StringUtils.isNotEmpty(basePath)) {
            imgName = basePath + BaseConstant.DEFAULT_URL_SEPARATOR + imgName;
        }
        Map<String, Object> result = qiNiuUploadUtil.uploadImg(file, imgName);
        UploadImgResDto uploadImgResDto = new UploadImgResDto();
        uploadImgResDto.setImgName(imgName);
        if (BaseConstant.SUCCESS_CODE == (result.get(BaseConstant.STR_CODE))) {
            uploadImgResDto.setImgUrl(domain + BaseConstant.DEFAULT_URL_SEPARATOR + qiNiuUploadUtil.getDownloadUrl(imgName));
        }
        return QueryResponse.setResData(uploadImgResDto, (Integer) result.get(BaseConstant.STR_CODE), String.valueOf(result.get(BaseConstant.STR_MSG)));
    }


    @Override
    public QueryRes uploadPictureSop(MultipartFile file, Integer imgMaxSize, String basePath, String imgPath) throws Exception{
        if (null == file) {
            return new QueryRes(SopImgError.ERROR_NO_SELECTED_TYPE.getCode(), SopImgError.ERROR_NO_SELECTED_TYPE.getMsg());
        }
        if (!checkImgSize(file,imgMaxSize)) {
            return new QueryRes(SopImgError.ERROR_IMG_SIZE.getCode(), SopImgError.ERROR_IMG_SIZE.getMsg());
        }
        String fileName = file.getOriginalFilename();  //文件名字
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String imgName = snowFlakeUtil.nextId() + suffix;  //生成新的文件名
        //文件名前加上配置的路径
        if (StringUtils.isNotEmpty(imgPath)) {
            imgName = imgPath + BaseConstant.DEFAULT_URL_SEPARATOR + imgName;
        }
        imgName = DateUtil.ChangeDateToString(new Date(), DateUtil.dateFormat4) + BaseConstant.DEFAULT_URL_SEPARATOR + imgName;
        if (StringUtils.isNotEmpty(basePath)) {
            imgName = basePath + BaseConstant.DEFAULT_URL_SEPARATOR + imgName;
        }
        Map<String, Object> result = qiNiuUploadUtil.uploadImg(file, imgName);
        UploadImgResDto uploadImgResDto = new UploadImgResDto();
        uploadImgResDto.setImgName(imgName);
        if (BaseConstant.SUCCESS_CODE == (result.get(BaseConstant.STR_CODE))) {
            uploadImgResDto.setImgUrl(domain + BaseConstant.DEFAULT_URL_SEPARATOR + qiNiuUploadUtil.getDownloadUrl(imgName));
        }
        return QueryRes.setResData(uploadImgResDto, (Integer) result.get(BaseConstant.STR_CODE), String.valueOf(result.get(BaseConstant.STR_MSG)));
    }

    /**
     * 图片大小校验
     * @return true：
     */
    public boolean checkImgSize(MultipartFile file,Integer imgMaxSize) {
        boolean result = false;
        if (0 == imgMaxSize) {
            return true;
        }
        if (file.getSize() <= imgMaxSize) {
            result = true;
        }
        return result;
    }
}
